﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WorkingTime.Classes.Model
{
    public class Project : NotifyPropertyChangedClass
    {
        private ObservableCollection<WorkTime> workTimes;
        private TimeSpan totalTime;
        public string Name { get; set; }

        public TimeSpan TotalTime
        {
            get { return totalTime; }
            set
            {
                totalTime = value;
                this.OnPropertyChanged(nameof(TotalTime));
            }
        }

        public ObservableCollection<WorkTime> WorkTimes
        {
            get { return workTimes; }
            set
            {
                workTimes = value;
                this.TotalTime = this.GetTotalTime();
            }
        }

        public Project(string name)
        {
            this.Name = name;
            this.WorkTimes = new ObservableCollection<WorkTime>();
        }

        ~Project()
        {
            if (!string.IsNullOrEmpty(this.Name))
            {
                this.SaveProject();
            }
        }

        public void AddWorkTime(WorkTime time)
        {
            this.WorkTimes.Add(time);
            this.TotalTime = GetTotalTime();
        }

        public void RefreshTotalTime()
        {
            this.TotalTime = GetTotalTime();
        }

        private void SaveProject()
        {
            using (StreamWriter sw = new StreamWriter(this.Name + ".proj"))
            {
                sw.WriteLine(this.Name);

                foreach (var workTime in this.WorkTimes)
                {
                    if (workTime.StopTime == null)
                    {
                        workTime.StopTime = DateTime.Now;
                    }

                    sw.WriteLine($"{workTime.StartTime}, {workTime.StopTime}");
                }
            }
        }

        private TimeSpan GetTotalTime()
        {
            TimeSpan result = TimeSpan.Zero;
            foreach (var time in this.WorkTimes)
                result = result + time.Duration;
            return result;
        }

        public override string ToString()
        {
            return this.Name;
        }

        public void DeleteProject()
        {
            File.Delete(this.Name + ".proj");
            this.Name = null;
        }

        public static List<Project> ReadProjects()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            List<Project> projects = new List<Project>();

            foreach (var file in dirInfo.GetFiles().Where(x => x.Extension.Contains("proj")))
            {
                using (StreamReader sr = new StreamReader(file.FullName))
                {
                    var project = new Project(sr.ReadLine());

                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine();
                        var startTime = line.Split(',')[0];
                        var stopTime = line.Split(',')[1];
                        var start = Convert.ToDateTime(startTime);
                        var stop = Convert.ToDateTime(stopTime);
                        var workTime = new WorkTime();
                        workTime.SetStartTime(start);
                        workTime.SetStopTime(stop);
                        project.AddWorkTime(workTime);
                        
                    }

                    projects.Add(project);
                }
            }

            return projects;
        }
    }
}
